﻿using System.IO;
using System.Text;

namespace ShuttleBus.Transports.AmazonSqs
{
    public class SqsMessage : BaseMessage
    {
        private readonly Amazon.SQS.Model.Message _message;
        private StreamWrapper _memoryStream;
        private readonly string _messageBody;
        private readonly int _recieveCount;

        public SqsMessage(Amazon.SQS.Model.Message message)
        {
            _message = message;
            _messageBody = _message.Body;
            int.TryParse(message.Attribute[0].Value, out _recieveCount);
        }

        public override string Id
        {
            get { return _message.ReceiptHandle; }
        }

        public override IStream Contents
        {
            get
            {
                if (_memoryStream == null)
                {
                    _memoryStream = new StreamWrapper(new MemoryStream());
                    byte[] data = Encoding.UTF8.GetBytes(_messageBody);
                    _memoryStream.Write(data, 0, data.Length);
                    _memoryStream.Position = 0;
                }
                return _memoryStream;
            }
        }

        public override string ResponseQueue
        {
            get { return string.Empty; }
        }

        public override int RecieveCount
        {
            get { return _recieveCount; }
        }

        public override object OriginalMessage
        {
            get { return _message; }
        }
    }
}